package user

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type ModifyVipLevelInfosLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员VIP等级信息 - 修改
func NewModifyVipLevelInfosLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyVipLevelInfosLogic {
	return &ModifyVipLevelInfosLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ModifyVipLevelInfosLogic) ModifyVipLevelInfos(req *types.ModifyVIPLevelInfosRebateRateRequest) (resp *types.ModifyVIPLevelInfosRebateRateResponse, err error) {
	// 对传递参数做简单逻辑校验
	if req.VipLevelId == 0 {
		err := errors.New("VipLevelId is empty")
		logx.Errorf("Call ModifyVipLevelInfos error: %v", err)
		return nil, err
	}

	if len(req.RebateRateList) == 0 {
		err := errors.New("RebateRateList is empty")
		logx.Errorf("Call ModifyVipLevelInfos error: %v", err)
		return nil, err
	}

	// 组合请求结构体
	// 组装返利率数据结构体
	rebateRateList := make([]*userrpc.RebateRate, len(req.RebateRateList))
	for j, rebateRate := range req.RebateRateList {
		rebateRateList[j] = &userrpc.RebateRate{
			RebateRateId: rebateRate.RebateRateId,
			TypeName:     rebateRate.TypeName,
			Name:         rebateRate.Name,
			Value:        rebateRate.Value,
		}
	}

	rpc_req := &userrpc.ModifyVIPLevelInfosRebateRateRequest{
		VipLevelId:          req.VipLevelId,
		RebateRateList:      rebateRateList,
		UpgradeExp:          req.UpgradeExp,
		KeepGradeExp:        req.KeepGradeExp,
		DiscountExp:         req.DiscountExp,
		UpgradePointAward:   req.UpgradePointAward,
		UpgradeBalanceAward: req.UpgradeBalanceAward,
		MonthlyPointAward:   req.MonthlyPointAward,
		MonthlyBalanceAward: req.MonthlyBalanceAward,
		RechargeAward:       req.RechargeAward,
		SafeDailyRate:       req.SafeDailyRate,
	}

	// 发送请求
	_, err = l.svcCtx.UserServiceRPC.UserModifyVipLevelInfosRebateRate(l.ctx, rpc_req)
	if err != nil {
		logx.Errorf("Call ModifyVipLevelInfos error: %v", err)
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.ModifyVIPLevelInfosRebateRateResponse{}
	return
}
